package hairmember

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/hairmember"
	hairmemberReq "github.com/flipped-aurora/gin-vue-admin/server/model/hairmember/request"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type MembersApi struct{}

// CreateMembers 创建members表
// @Tags Members
// @Summary 创建members表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body hairmember.Members true "创建members表"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /members/createMembers [post]
func (membersApi *MembersApi) CreateMembers(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var members hairmember.Members
	err := c.ShouldBindJSON(&members)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = membersService.CreateMembers(ctx, &members)
	if err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

// DeleteMembers 删除members表
// @Tags Members
// @Summary 删除members表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body hairmember.Members true "删除members表"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /members/deleteMembers [delete]
func (membersApi *MembersApi) DeleteMembers(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	id := c.Query("id")
	err := membersService.DeleteMembers(ctx, id)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteMembersByIds 批量删除members表
// @Tags Members
// @Summary 批量删除members表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /members/deleteMembersByIds [delete]
func (membersApi *MembersApi) DeleteMembersByIds(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	ids := c.QueryArray("ids[]")
	err := membersService.DeleteMembersByIds(ctx, ids)
	if err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// UpdateMembers 更新members表
// @Tags Members
// @Summary 更新members表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body hairmember.Members true "更新members表"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /members/updateMembers [put]
func (membersApi *MembersApi) UpdateMembers(c *gin.Context) {
	// 从ctx获取标准context进行业务行为
	ctx := c.Request.Context()

	var members hairmember.Members
	err := c.ShouldBindJSON(&members)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	err = membersService.UpdateMembers(ctx, members)
	if err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindMembers 用id查询members表
// @Tags Members
// @Summary 用id查询members表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param id query int true "用id查询members表"
// @Success 200 {object} response.Response{data=hairmember.Members,msg=string} "查询成功"
// @Router /members/findMembers [get]
func (membersApi *MembersApi) FindMembers(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	id := c.Query("id")
	remembers, err := membersService.GetMembers(ctx, id)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:"+err.Error(), c)
		return
	}
	response.OkWithData(remembers, c)
}

// GetMembersList 分页获取members表列表
// @Tags Members
// @Summary 分页获取members表列表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data query hairmemberReq.MembersSearch true "分页获取members表列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /members/getMembersList [get]
func (membersApi *MembersApi) GetMembersList(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var pageInfo hairmemberReq.MembersSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := membersService.GetMembersInfoList(ctx, pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// GetMemberRechargeList 获取会员充值记录列表
// @Tags Members
// @Summary 获取会员充值记录列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query request.PageInfo true "获取会员充值记录列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /members/getMemberRechargeList [get]
// @Param data query request.MemberConsumptionQuery true "获取会员充值记录列表"
func (membersApi *MembersApi) GetMemberRechargeList(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var query request.MemberConsumptionQuery
	err := c.ShouldBindQuery(&query)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	list, total, err := membersService.GetMemberRechargeList(ctx, query)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     query.Page,
		PageSize: query.PageSize,
	}, "获取成功", c)
}

// RechargeMember 会员充值
// @Tags Members
// @Summary 会员充值
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body model.MemberRecharge true "会员充值"
// @Success 200 {object} response.Response{msg=string} "充值成功"
// @Router /members/rechargeMember [post]
func (membersApi *MembersApi) RechargeMember(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var recharge model.MemberRecharge
	err := c.ShouldBindJSON(&recharge)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	// 获取当前用户ID
	userID := c.GetUint("userId")

	err = membersService.RechargeMember(ctx, recharge, uint64(userID))
	if err != nil {
		global.GVA_LOG.Error("充值失败!", zap.Error(err))
		response.FailWithMessage("充值失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("充值成功", c)
}

// ConsumeMember 会员消费扣费
// @Tags Members
// @Summary 会员消费扣费
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body model.MemberConsumption true "会员消费"
// @Success 200 {object} response.Response{msg=string} "扣费成功"
// @Router /members/consumeMember [post]
func (membersApi *MembersApi) ConsumeMember(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var consumption model.MemberConsumption
	err := c.ShouldBindJSON(&consumption)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	// 获取当前用户ID
	userID := c.GetUint("userId")

	err = membersService.ConsumeMember(ctx, consumption, uint64(userID))
	if err != nil {
		global.GVA_LOG.Error("扣费失败!", zap.Error(err))
		response.FailWithMessage("扣费失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("扣费成功", c)
}

// GetMemberConsumptionList 获取会员消费记录列表
// @Tags Members
// @Summary 获取会员消费记录列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query request.MemberConsumptionQuery true "获取会员消费记录列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /members/getMemberConsumptionList [get]
func (membersApi *MembersApi) GetMemberConsumptionList(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	var query request.MemberConsumptionQuery
	err := c.ShouldBindQuery(&query)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	list, total, err := membersService.GetMemberConsumptionList(ctx, query)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     query.Page,
		PageSize: query.PageSize,
	}, "获取成功", c)
}

// GetMembersPublic 不需要鉴权的members表接口
// @Tags Members
// @Summary 不需要鉴权的members表接口
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=object,msg=string} "获取成功"
// @Router /members/getMembersPublic [get]
func (membersApi *MembersApi) GetMembersPublic(c *gin.Context) {
	// 创建业务用Context
	ctx := c.Request.Context()

	// 此接口不需要鉴权
	// 示例为返回了一个固定的消息接口，一般本接口用于C端服务，需要自己实现业务逻辑
	membersService.GetMembersPublic(ctx)
	response.OkWithDetailed(gin.H{
		"info": "不需要鉴权的members表接口信息",
	}, "获取成功", c)
}
